from .user_info import User
from db_connect.datetimes import DateEncoder
from sanic import Sanic
from sanic import response
from sanic import Blueprint
from . import app_user

users = User()


@app_user.route('/merchant')
async def merchants(request):
    #查询商家的信息
    info = request.args
    user_phone = info.get('user_phone')
    result_info = await users.merchant_info(user_phone)
    return response.json(result_info)


@app_user.route('/order')
async def orders(request):
    #查询用户的订单的信息
    info = request.args
    user_id = info.get('user_id')
    result_info = await users.order_info(user_id)
    return response.json(result_info, cls=DateEncoder)


@app_user.route('/submit_order', methods=['POST'])
async def submit(request):
    #用户提交订单
    info = request.form
    user_id = info.get('user_id')
    merchat_id = info.get('merchat_id')
    orders = info.get('orders')
    # status = info.get('status')
    result_info = await users.submit_order_info(
        user_id,
        merchat_id,
        orders,
    )
    print(result_info)
    return response.json(result_info, cls=DateEncoder)


@app_user.route('/change_order')
async def change(request):
    #改变用户订单的状态
    info = request.args
    status = info.get('status')
    order_id = info.get('order_id')
    result_info = await users.change_order_info(status, order_id)
    print(result_info)
    return response.json(result_info)
